//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.tuliu.appupdate.updatelib;

import android.content.Intent;
import android.os.IBinder;
import android.text.TextUtils;
import android.util.Log;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

abstract class MultiIntentService extends BaseService {
    private boolean mRedelivery;
    private ThreadPoolExecutor mExecutor;
    private String mName;

    public MultiIntentService(String name) {
        this.mName = name;
    }

    public void setIntentRedelivery(boolean enabled) {
        this.mRedelivery = enabled;
    }

    public void onCreate() {
        super.onCreate();
        if(TextUtils.isEmpty(this.mName)) {
            this.mExecutor = new ThreadPoolExecutor(this.getCorePoolSize(), this.getMaximumPoolSize(), 60L, TimeUnit.SECONDS, new LinkedBlockingQueue(), new MultiIntentService.DefaultThreadFactory("MultiIntentService"));
        } else {
            this.mExecutor = new ThreadPoolExecutor(this.getCorePoolSize(), this.getMaximumPoolSize(), 60L, TimeUnit.SECONDS, new LinkedBlockingQueue(), new MultiIntentService.DefaultThreadFactory(this.mName));
        }

    }

    protected int getCorePoolSize() {
        return 1000;
    }

    protected int getMaximumPoolSize() {
        return 2147483647;
    }

    public int getActiveCount() {
        return this.mExecutor.getActiveCount();
    }

    public long getCompletedTaskCount() {
        return this.mExecutor.getCompletedTaskCount();
    }

    public long getTaskCount() {
        return this.mExecutor.getTaskCount();
    }

    public BlockingQueue<Runnable> getTaskQueue() {
        return this.mExecutor.getQueue();
    }

    public void onStart(final Intent intent, final int startId) {
        this.mExecutor.submit(new Runnable() {
            public void run() {
                try {
                    MultiIntentService.this.onHandleIntent(intent);
                } catch (Throwable var5) {
                    Log.e("MultiIntentService", "onHandleIntent", var5);
                } finally {
                    MultiIntentService.this.stopSelf(startId);
                }

            }
        });
    }

    public int onStartCommand(Intent intent, int flags, int startId) {
        this.onStart(intent, startId);
        return this.mRedelivery?3:2;
    }

    public void onDestroy() {
        this.mExecutor.shutdownNow();
        super.onDestroy();
    }

    public IBinder onBind(Intent intent) {
        return null;
    }

    protected abstract void onHandleIntent(Intent var1);

    private static class DefaultThreadFactory implements ThreadFactory {
        private static final AtomicInteger poolNumber = new AtomicInteger(1);
        private final ThreadGroup group;
        private final AtomicInteger threadNumber = new AtomicInteger(1);
        private final String namePrefix;

        DefaultThreadFactory(String name) {
            SecurityManager s = System.getSecurityManager();
            this.group = s != null?s.getThreadGroup():Thread.currentThread().getThreadGroup();
            this.namePrefix = name + "-pool-" + poolNumber.getAndIncrement() + "-thread-";
        }

        public Thread newThread(Runnable r) {
            Thread t = new Thread(this.group, r, this.namePrefix + this.threadNumber.getAndIncrement(), 0L);
            if(t.isDaemon()) {
                t.setDaemon(false);
            }

            if(t.getPriority() != 5) {
                t.setPriority(5);
            }

            return t;
        }
    }
}
